package com.example.pruebaflickr;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.test.TestInterface;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends Activity {
	ImageView image;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		image=(ImageView) findViewById(R.id.imageView1);

		// Flickr flickr = new Flickr(API_KEY,SHARED_SECRET,new REST());
		// Set<String> extras = new HashSet();
		//
		// // A set of extra info we want Flickr to give back. Go to the API
		// page to see the other size options available.
		// extras.add("url_o");
		// extras.add("original_format");
		//
		// //A request for a list of the photos in a set. The first zero is the
		// privacy filter,
		// // the second is the Pages, and the third is the Per-Page (see the
		// Flickr API)
		//
		// PhotoList<Photo> photoList =
		// flickr.getPhotosetsInterface().getPhotos(PHOTOSET_ID, extras, 0, 0,
		// 0);
		//
		//
		// //We'll use the direct URL to the original size of the photo in order
		// to download it. Remember: you want to make as few requests from
		// flickr as possible!
		//
		// for(Photo photo : photoList){
		// //You can also get other sizes. Just ask for the info in the first
		// request.
		// URL url = new URL(photo.getOriginalSize().getSource());
		//
		//
		//
		// InputStream is = url.openStream();
		// OutputStream os = new FileOutputStream(PATH_OF_FOLDER +
		// photo.getTitle() + "." + photo.getOriginalFormat());
		//
		// byte[] b = new byte[2048];
		// int length;
		//
		// while ((length = is.read(b)) != -1) {
		// os.write(b, 0, length);
		// }
		//
		// is.close();
		// os.close();
		//new Thread(getMetadata).start();
		
		new DownloadImageTask(image)
		.execute("https://farm4.staticflickr.com/3910/14992723487_4808673a05.jpg");
        //.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
		
		
		
		
		
		
		
//		runOnUiThread(new Runnable() {  
//            @Override
//            public void run() {
//                // TODO Auto-generated method stub
//
//            	Bitmap bm = null;
//				try {
//					Log.e("MainActivity", "1");
//					URL aURL = new URL(
//							"https://www.flickr.com/photos/29096781@N02/3606436456");
//					URLConnection conn = aURL.openConnection();
//					Log.e("MainActivity", "2");
//					conn.connect();
//					Log.e("MainActivity", "3");
//					InputStream is = conn.getInputStream();
//					Log.e("MainActivity", "4");
//					BufferedInputStream bis = new BufferedInputStream(is);
//					Log.e("MainActivity", "5");
//					bm = BitmapFactory.decodeStream(bis);
//					Log.e("MainActivity", "6");
//					bis.close();
//					is.close();
//				} catch (Exception e) {
//					Log.e("MainActivity", "10");
//					if (e==null) {
//						Log.e("MainActivity", "11");	
//					}
//					else{
//						Log.e("FlickrManager", "12");	
//					}
//					
//					Log.e("MainActivity", "13");
//				}
//				
//				Log.e("MainActivity", "20");
//				image.setImageBitmap(bm);
//            }
//        });
		

		
		
		
		
		
		
		
		// String apiKey = "a0931fa97ea9aa0b52fe1688d7d904a9";
		// Flickr f = new Flickr(apiKey);
		// TestInterface testInterface = f.getTestInterface();
		// Collection results = testInterface.echo(Collections.EMPTY_LIST);

	}
	
	
	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
	    ImageView bmImage;
	    

	    public DownloadImageTask(ImageView bmImage) {
	        this.bmImage = bmImage;
	    }

	    protected Bitmap doInBackground(String... urls) {
	        String urldisplay = urls[0];
	        Bitmap mIcon11 = null;
	        try {
	            InputStream in = new java.net.URL(urldisplay).openStream();
	            mIcon11 = BitmapFactory.decodeStream(in);
	        } catch (Exception e) {
	            Log.e("Error", e.getMessage());
	            e.printStackTrace();
	        }
	        return mIcon11;
	    }

	    protected void onPostExecute(Bitmap result) {
	        bmImage.setImageBitmap(result);
	    }
	}

	/**
	 * Runnable to get metadata from Flickr API
	 */
	Runnable getMetadata = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			
			Bitmap bm = null;
			try {
				Log.e("MainActivity", "1");
				URL aURL = new URL(
						"https://www.flickr.com/photos/29096781@N02/3606436456");
				URLConnection conn = aURL.openConnection();
				Log.e("MainActivity", "2");
				conn.connect();
				Log.e("MainActivity", "3");
				InputStream is = conn.getInputStream();
				Log.e("MainActivity", "4");
				BufferedInputStream bis = new BufferedInputStream(is);
				Log.e("MainActivity", "5");
				bm = BitmapFactory.decodeStream(bis);
				Log.e("MainActivity", "6");
				bis.close();
				is.close();
			} catch (Exception e) {
				Log.e("MainActivity", "10");
				if (e==null) {
					Log.e("MainActivity", "11");	
				}
				else{
					Log.e("FlickrManager", "12");	
				}
				
				Log.e("MainActivity", "13");
			}
			
			Log.e("MainActivity", "20");
			image.setImageBitmap(bm);
			
		}
	};
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
